<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'pieChart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      loading: true,
      chart: null
    }
  },
  mounted() {
    this.loading = true
    this.initChart()
  },
  beforeDestroy() {
    this.destroyChart()
  },
  methods: {
    handleRefresh(legendData, countList) {
      const option = this.getPieOptions(legendData, countList)
      this.chart.setOption(option)
      this.loading = false
    },
    destroyChart() {
      if (!this.chart) return
      this.chart.dispose()
      this.chart = null
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
    },
    getPieOptions(legendData, countList) {
      // console.log('pieData', legendData, countList)
      const data = []
      for (let i = 0; i < legendData.length; i++) {
        data.push({ value: countList[i], name: legendData[i] })
      }
      return {
        title: {
          text: '设备状态饼图',
          left: 'center',
          textStyle: {
            color: '#000'
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10'
        },
        series: [
          {
            name: '最新设备状态饼图',
            type: 'pie',
            roseType: 'radius',
            radius: [20, 95],
            center: ['50%', '38%'],
            data: data,
            animationEasing: 'cubicInOut',
            animationDuration: 1000
          }
        ]
      }
    }
  }
}
</script>
